<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <title>购买计次卡</title>
    <link rel="stylesheet" href="__STATIC_URL__/static/mobile/css/weui.css"/>
    <link rel="stylesheet" href="__STATIC_URL__/static/mobile/css/cardjc.css" />

</head>
<body>

<div class="container" id="container">

    <div class="top">
        <div class="header">
            <img src="{$info['logo_url']}" mode='aspectFill' class='shopLogo'/>
            <p class="name">{$info['brand_name']}</p>
            <div style="clear:both;"></div>
        </div>
        <div class="title">
            {$info['title']}
            <p class="pic">{$info['price']}元{$info['pay_num']}次</p>
        </div>
        <div class="btn">
            <input type="hidden" id="price" value="{$info['price']}">
            <input type="hidden" id="mid" value="{$info['mid']}">
            <input type="hidden" id="orderid" value="">
            <input type="hidden" id="openid" value="{$info['openid']}">
            <input type="hidden" id="tims_id" value="{$info['id']}">
            <input type="hidden" id="card_id" value="{$info['card_id']}">

            <buton class="weui-btn weui-btn_primary">立即购买</buton>
        </div>
    </div>


    <div class="body">
        <div class="semi_circle"></div>
        <div class="semi_circle1"></div>
        <div class="weui-flex">
            <div>使用说明:</div>
            <div class="weui-flex__item">有效期内使用</div>
        </div>
        <div class="weui-flex">
            <div>有效时间:</div>
            <div class="weui-flex__item">领取后{$info['term_of']}天以内。</div>
        </div>

    </div>

</div>
</body>
<script src="__STATIC_URL__/static/jquery/3.2.1/jquery.min.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="__STATIC_URL__/static/layer/3.0.3/layer.js"></script>
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script type="text/javascript">
    wx.config({$share.wxjssdk});
    wx.ready(function () {
        wx.checkJsApi({
            jsApiList: [
                'onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ',
                'onMenuShareWeibo','onMenuShareQZone'
            ],
        });
        var sharedata = {
            title:"{$info.brand_name}-{$info.title}",
            desc:"{$info.brand_name}-{$info.title}",
            link: "https://os.wunengkeji.com/mobile.php/cardjc/cardjclist/card_id/{$info.card_id}",
            imgUrl: "{$info.logo_url}",
        };
        wx.onMenuShareAppMessage(sharedata);
        wx.onMenuShareTimeline(sharedata);
        wx.onMenuShareQQ(sharedata);
        wx.onMenuShareWeibo(sharedata);
        wx.onMenuShareQZone(sharedata);

    })
</script>
<script>

    $("#Refresh").click(function(){
        window.location.reload();
    })
</script>
<script type="text/javascript">

    function getorder(mid){
        var id = mid;
        $.ajax({
            type: "POST",
            url: "{:url('cardjc/getorder2')}",
            cache: false,
            async: false,
            data: {
                id: id,
            },
            success: function (data) {
                $('#orderid').val(data);

            }
        });
    }
    $(".weui-btn_primary").click(function () {
        var price = $('#price').val();
        var mid = $('#mid').val();
        getorder(mid);
        var openid = $('#openid').val();
        var id = $('#tims_id').val();
        var orderid = $('#orderid').val();
        var card_id = $('#card_id').val();
        $.ajax({
            url: "{:url('cardjc/pay')}",
            type: 'post',
            dataType: 'json',
            data: {
                price: price,
                mid: mid,
                tims_id: id,
                openid: openid,
                orderid: orderid,
                card_id: card_id,

            },
            success: function (data) {
                console.log(132,data)
                // return;
                if(data.code!=0){
                    layer.msg(data.message);
                    $("#iosDialog3").fadeIn(200);
                }
                if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                    } else if (document.attachEvent) {
                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                    }
                } else {
                    onBridgeReady(data);
                }
            }
        })
    })
    function onBridgeReady(sdata) {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest', sdata,
            function (res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    paysuccess();
                }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
            }
        );
    };
    function paysuccess() {
        $.ajax({
            type: "POST",
            url: "{:url('cardjc/r_payover')}",
            cache: false,
            dataType: "json",
            async: false,
            data: {
                id:{$info['mid']},
                orderid:$('#orderid').val(),
                cardid: $('#cardid').val(),
            },
            success: function (data) {
                if (data.code == 0) {
                    layer.msg(data.message);
                    setTimeout(function(){
                        window.location.href = data.url;
                    },500)
                } else {
                    layer.msg(data.message);

                }
            }


        });
    }
</script>

</html>
